package com.heima.gateway.filter;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.RequestPath;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: Serendipity-li
 * @Date: 2024/8/1 - 8:26
 * @Description: 自定义一个带有参数的过滤器
 */
@Component
public class PrintAnyGatewayFilter extends AbstractGatewayFilterFactory<PrintAnyGatewayFilter.Config> {


    @Override
    public GatewayFilter apply(Config config) {
        return new OrderedGatewayFilter((exchange,chain)->{
            RequestPath path = exchange.getRequest().getPath();
            System.out.println("path = " + path);
            System.out.println(config.getA()+config.getB()+ config.getC());
            return chain.filter(exchange);
        },1);
    }

    /*
     * @description:以下内容是若是带参数的过滤器的话，改如何定义
     * @author: louis-li
     * @date: 2024/8/1 8:58
     * @param:
     * @return:
     **/

    //自定义配置属性，成员变量名称很重要，下面会用到
    @Data
    public  static final  class Config{
        private String a;
        private String b;
        private String c;
    }

    //将变量名称依次返回，顺序很重要，将来读取参数时需要按顺序获取
    @Override
    public List<String> shortcutFieldOrder() {
        return List.of("a","b","c");
    }

    //返回当前配置类的类型，也就是内部的Config
    public PrintAnyGatewayFilter() {
        super(Config.class);
    }
}
